package com.bw.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import com.bw.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/sys")
public class LoginController {

    @Autowired
    StringRedisTemplate redisTemplate;

    @RequestMapping("getPicCode")
    public void picCode(String uuid, HttpServletResponse response) throws IOException {

        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 50);

        String code = lineCaptcha.getCode();
        redisTemplate.opsForValue().set("captcha:" + uuid, code, 3, TimeUnit.MINUTES);
        lineCaptcha.write(response.getOutputStream());
    }

    @RequestMapping("test2")
    public Result test2() {

        return Result.ok("test success");
    }

    @GetMapping("login22")
    public Result login() {
        log.info("------login is called-------");
        return Result.fail("test success");
    }


}
